import { ref } from 'vue'

export default function useTable(listApi) {
  const loading = ref(false)
  const list = ref([])
  const total = ref(0)
  const queryParams = ref({
    pageNum: 1,
    pageSize: 10
  })

  // 获取列表数据
  const getList = async () => {
    loading.value = true
    try {
      const res = await listApi(queryParams.value)
      list.value = res.data.records
      total.value = res.data.total
    } catch (error) {
      console.error(error)
    } finally {
      loading.value = false
    }
  }

  // 重置查询参数
  const resetQuery = () => {
    queryParams.value = {
      pageNum: 1,
      pageSize: 10
    }
    getList()
  }

  // 处理页码改变
  const handleCurrentChange = (val) => {
    queryParams.value.pageNum = val
    getList()
  }

  // 处理每页条数改变
  const handleSizeChange = (val) => {
    queryParams.value.pageSize = val
    queryParams.value.pageNum = 1
    getList()
  }

  return {
    loading,
    list,
    total,
    queryParams,
    getList,
    resetQuery,
    handleCurrentChange,
    handleSizeChange
  }
} 